import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import PhotographersView from '@/views/PhotographersView.vue'
import PortfolioView from '@/views/PortfolioView.vue'
import AboutView from '@/views/AboutView.vue'
import BookingView from '@/views/BookView.vue'
import { useUserStore } from '@/stores/user'
import AdminLayout from '@/components/AdminLayout.vue'

const frontendRoutes = [
  {
    path: '/',
    name: 'home',
    component: HomeView,
    meta: {
      title: '首页',
      hidePageHeader: true,
    },
  },
  {
    path: '/photographers',
    name: 'photographers',
    component: PhotographersView,
    meta: {
      title: '摄影师',
      subtitle: '选择您心仪的摄影师',
    },
  },
  {
    path: '/portfolio',
    name: 'portfolio',
    component: PortfolioView,
    meta: {
      title: '客照欣赏',
      subtitle: '欣赏我们的精选婚纱摄影作品',
    },
  },
  {
    path: '/portfolio/:id',
    name: 'photographerPortfolio',
    component: () => import('@/views/PhotographerPortfolioView.vue'),
    meta: {
      title: '摄影师作品集',
    },
  },
  {
    path: '/about',
    name: 'about',
    component: AboutView,
    meta: {
      title: '关于我们',
      subtitle: '了解我们的团队和服务',
    },
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/LoginView.vue'),
    meta: {
      title: '登录',
      subtitle: '登录您的账户以预约拍摄',
    },
  },
  {
    path: '/booking',
    name: 'booking',
    component: BookingView,
    meta: {
      requiresAuth: true,
      title: '预约拍摄',
      subtitle: '选择您心仪的套餐和时间',
    },
  },
  {
    path: '/profile',
    name: 'profile',
    component: () => import('@/views/ProfileView.vue'),
    meta: {
      requiresAuth: true,
      title: '个人中心',
      subtitle: '',
    },
  },
  {
    path: '/profile/info',
    name: 'profileInfo',
    component: () => import('@/views/ProfileInfoView.vue'),
    meta: {
      requiresAuth: true,
      title: '个人信息',
      subtitle: '',
    },
  },
  {
    path: '/customer-manage',
    name: 'customerManage',
    component: () => import('@/views/CustomerManageView.vue'),
    meta: {
      requiresAuth: true,
      title: '客户列表',
      requiresAdmin: true, // 需要管理员权限
    },
  },
  {
    path: '/photographer-manage',
    name: 'photographerManage',
    component: () => import('@/views/PhotographerManageView.vue'),
    meta: {
      requiresAuth: true,
      title: '摄影师列表',
      requiresAdmin: true,
    },
  },
  {
    path: '/admin-manage',
    name: 'adminManage',
    component: () => import('@/views/AdminManageView.vue'),
    meta: {
      requiresAuth: true,
      title: '管理员列表',
      requiresSuperAdmin: true, // 仅超级管理员可访问
    },
  },
  {
    path: '/dress-manage',
    name: 'dressManage',
    component: () => import('@/views/DressManageView.vue'),
    meta: {
      requiresAuth: true,
      title: '婚纱列表',
      requiresAdmin: true,
    },
  },
  {
    path: '/booking-manage',
    name: 'bookingManage',
    component: () => import('@/views/BookingManageView.vue'),
    meta: {
      requiresAuth: true,
      title: '预约列表',
      requiresAdmin: true,
    },
  },
  {
    path: '/order-manage',
    name: 'orderManage',
    component: () => import('@/views/OrderManageView.vue'),
    meta: {
      requiresAuth: true,
      title: '订单列表',
      requiresAdmin: true,
    },
  },
  {
    path: '/sample-manage',
    name: 'sampleManage',
    component: () => import('@/views/SampleManageView.vue'),
    meta: {
      requiresAuth: true,
      title: '样片列表',
      requiresAdmin: true,
    },
  },
  {
    path: '/package-manage',
    name: 'packageManage',
    component: () => import('@/views/PackageManageView.vue'),
    meta: {
      requiresAuth: true,
      title: '套餐列表',
      requiresAdmin: true,
    },
  },
  {
    path: '/review-manage',
    name: 'reviewManage',
    component: () => import('@/views/ReviewManageView.vue'),
    meta: {
      requiresAuth: true,
      title: '评价列表',
    },
  },
  {
    path: '/banner-manage',
    name: 'bannerManage',
    component: () => import('@/views/BannerManageView.vue'),
    meta: {
      requiresAuth: true,
      title: 'Banner 管理',
      requiresAdmin: true,
    },
  },
  {
    path: '/order/:id',
    name: 'orderDetail',
    component: () => import('@/views/OrderDetailView.vue'),
    meta: {
      requiresAuth: true,
      title: '订单详情',
    },
  },
  {
    path: '/booking/:id',
    name: 'bookingDetail',
    component: () => import('@/views/BookingDetailView.vue'),
    meta: {
      requiresAuth: true,
      title: '预约详情',
    },
  },
  {
    path: '/payment/:id',
    name: 'payment',
    component: () => import('@/views/PaymentView.vue'),
    meta: {
      requiresAuth: true,
      title: '订单支付',
    },
  },
  {
    path: '/payment/:id/qrcode',
    name: 'paymentQRCode',
    component: () => import('@/views/PaymentQRCodeView.vue'),
    meta: {
      requiresAuth: true,
      title: '扫码支付',
    },
  },
]

// 后台管理路由
const adminRoutes = {
  path: '/admin',
  component: AdminLayout,
  meta: {
    requiresAuth: true,
  },
  children: [
    {
      path: '',
      redirect: '/admin/profile',
    },
    {
      path: 'profile',
      name: 'adminProfile',
      component: () => import('@/views/ProfileInfoView.vue'),
      meta: {
        requiresAuth: true,
        title: '个人资料',
      },
    },
    {
      path: 'customers',
      name: 'adminCustomers',
      component: () => import('@/views/CustomerManageView.vue'),
      meta: {
        requiresAuth: true,
        title: '客户管理',
        requiresAdmin: true,
      },
    },
    {
      path: 'photographers',
      name: 'adminPhotographers',
      component: () => import('@/views/PhotographerManageView.vue'),
      meta: {
        requiresAuth: true,
        title: '摄影师管理',
        requiresAdmin: true,
      },
    },
    {
      path: 'admins',
      name: 'adminAdmins',
      component: () => import('@/views/AdminManageView.vue'),
      meta: {
        requiresAuth: true,
        title: '管理员管理',
        requiresSuperAdmin: true,
      },
    },
    {
      path: 'bookings',
      name: 'adminBookings',
      component: () => import('@/views/BookingManageView.vue'),
      meta: {
        requiresAuth: true,
        title: '预约管理',
        requiresAdmin: true,
      },
    },
    {
      path: 'orders',
      name: 'adminOrders',
      component: () => import('@/views/OrderManageView.vue'),
      meta: {
        requiresAuth: true,
        title: '订单管理',
        requiresAdmin: true,
      },
    },
    {
      path: 'samples',
      name: 'adminSamples',
      component: () => import('@/views/SampleManageView.vue'),
      meta: {
        requiresAuth: true,
        title: '样片管理',
        requiresAdmin: true,
      },
    },
    {
      path: 'packages',
      name: 'adminPackages',
      component: () => import('@/views/PackageManageView.vue'),
      meta: {
        requiresAuth: true,
        title: '套餐管理',
        requiresAdmin: true,
      },
    },
    {
      path: 'reviews',
      name: 'adminReviews',
      component: () => import('@/views/ReviewManageView.vue'),
      meta: {
        requiresAuth: true,
        title: '评价管理',
      },
    },
    {
      path: 'banners',
      name: 'adminBanners',
      component: () => import('@/views/BannerManageView.vue'),
      meta: {
        requiresAuth: true,
        title: 'Banner管理',
        requiresAdmin: true,
      },
    },
    {
      path: 'dresses',
      name: 'adminDresses',
      component: () => import('@/views/DressManageView.vue'),
      meta: {
        requiresAuth: true,
        title: '婚纱管理',
        requiresAdmin: true,
      },
    },
  ],
}

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [...frontendRoutes, adminRoutes],
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const userStore = useUserStore()

  // 设置页面标题
  document.title = to.meta.title as string

  // 检查是否需要登录
  if (to.matched.some((record) => record.meta.requiresAuth)) {
    if (!userStore.isLoggedIn) {
      next({
        path: '/login',
        query: { redirect: to.fullPath },
      })
    } else {
      next()
    }
  } else {
    next()
  }
})

export default router
